﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

using EdgeStatz.ESSystems.Objects;
using EdgeStatz.ESSystems.Logs;

namespace EdgeStatz.ModelEngine.DatabaseTools
{
    public static class DataEngine
    {
        
        #region Import
        public static TableData[] Import(string xmlPath)
        {
            DEXMLReader xmlReader = new DEXMLReader(xmlPath);
            return xmlReader.DataTableArray;
        }

        public static string GetExpression(TableData data, int rowNumber, string expression)
        {
            if (rowNumber >= data.Rows.Length)
                throw new Exception("Row Number is greater than the number of datarows.");
            RowData row = data.Rows[rowNumber];
            return GetExpression(data, row, expression);
        }
        public static string GetExpression(TableData data, RowData row, string expression)
        {
            string retExp = "";
            if (expression.Length > 5 && expression.Substring(0, 5).ToUpper().Equals("EVAL("))
            {
                string[] expArray = expression.Substring(5, expression.Length - 6).Split('\'');
                for (int i = 0; i < expArray.Length; i++)
                {
                    if (i > 0 && i < expArray.Length)
                        retExp += " + ";
                    switch (i % 2)
                    {
                        case 0:
                            retExp += "\'" + expArray[i] + "\'";
                            break;
                        case 1:
                            if (expArray[i].Contains(".Prompt("))
                            {
                                string tabcol = expArray[i].Substring(0, expArray[i].IndexOf(".Prompt("));
                                string[] tmp = expArray[i].Substring(expArray[i].IndexOf(".Prompt(") + 8,
                                    expArray[i].Length - 1 - 8 - tabcol.Length).Split(',');
                                string reftab = (tmp[0].Split('.'))[0];
                                string refcol = (tmp[0].Split('.'))[1];
                                string rtcol = (tmp[1].Split('.'))[1];
                                retExp += "(cast([" + reftab + i + "].[" + rtcol + "] as varchar))";
                            }
                            else
                                retExp += "'" + row.Items[data.ColumnIndex(expArray[i])].ToString() + "'";
                            break;
                    }
                }
            }
            else
            {
                retExp = GlobalObjectsSQL.ReturnForDataEntry(row.Items[data.ColumnIndex(expression)]);
            }
            return retExp;
        }
        #endregion

        #region Export
        public static void Export(TableData[] data, string path)
        {
            DEXMLWriter xmlWriter = new DEXMLWriter(path);
            xmlWriter.TableData = data;

            xmlWriter.Write();
        }
        #endregion
    }
}
